package com.ruoyi.erp.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.apache.poi.util.StringUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.erp.domain.StaffProfile;
import com.ruoyi.erp.service.IStaffProfileService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 员工档案Controller
 * 
 * @author sauger
 * @date 2022-03-11
 */
@Controller
@RequestMapping("/erp/profile")
public class StaffProfileController extends BaseController
{
    private String prefix = "erp/profile";

    @Autowired
    private IStaffProfileService staffProfileService;

    @Autowired
    private  ISysUserService sysUserService;


    @RequiresPermissions("erp:profile:view")
    @GetMapping()
    public String profile()
    {
        return prefix + "/profile";
    }

    @RequiresPermissions("erp:profile:onboard")
    @PostMapping("/onboard_list")
    @ResponseBody
    public TableDataInfo onboardList(SysUser sysUser){
        startPage();
        List<SysUser> list = staffProfileService.selectUnonboardUser(sysUser);
        return getDataTable(list);
    }

    @RequiresPermissions("erp:profile:onboard")
    @GetMapping("/onboard")
    public String onboardView() { return prefix + "/index"; }

    @RequiresPermissions("erp:profile:onboard")
    @GetMapping("/onboard/{userId}")
    public String doOnboardView(@PathVariable("userId") Integer userId,ModelMap mmap)
    {
        SysUser user =  sysUserService.selectUserById(Long.valueOf(userId));
        if (StringUtils.isNull(user)) throw new ServiceException("账号不存在");
        StaffProfile staffProfile = new StaffProfile();
        staffProfile.setUserId(userId);
        staffProfile.setOnboardDate(DateUtils.getNowDate());
        mmap.put("staffProfile", staffProfile);
        mmap.put("user", user);
        return prefix + "/onboard";
    }

    /**
     * 离职处理,首页
     */
    @RequiresPermissions("erp:profile:depart")
    @GetMapping("/depart")
    public String departView() { return prefix + "/depart/index"; }

    /**
     * 返回在职员工列表数据
     * @param profile
     * @return
     */
    @RequiresPermissions("erp:profile:depart")
    @PostMapping("/depart/list")
    @ResponseBody
    public TableDataInfo departList(StaffProfile profile){
        startPage();
        profile.setOnboardStatus(Integer.parseInt(DictUtils.getDictValue("onboard_status","在职")));
        List<StaffProfile> list = staffProfileService.selectStaffProfileList(profile);
        return getDataTable(list);
    }


    @RequiresPermissions("erp:profile:depart")
    @GetMapping("/depart/{id}")
    public String viewDepart(@PathVariable("id") Integer id, ModelMap mmap)
    {
        StaffProfile exist = staffProfileService.selectStaffProfileById(id);
        if (StringUtils.isNull(exist)) throw new ServiceException("员工信息不存在！");
        mmap.put("staffProfile", exist);
        return prefix + "/depart/depart";
    }

    /**
     * 保存离职状态
     *
     * @param staffProfile
     * @return
     */
    @RequiresPermissions("erp:profile:depart")
    @PostMapping("/depart")
    @ResponseBody
    public AjaxResult saveDepart(StaffProfile staffProfile)
    {
        staffProfile.setUpdateBy(getLoginName());
        staffProfile.setOnboardStatus(Integer.parseInt(DictUtils.getDictValue("onboard_status","离职")));
        //禁用账号
        SysUser sysUser = sysUserService.selectUserById(Long.valueOf(staffProfile.getUserId()));
        if (!"1".equals(sysUser.getStatus())){
            sysUser.setStatus("1");
            sysUserService.updateUser(sysUser);
        }
        return toAjax(staffProfileService.updateStaffProfile(staffProfile));
    }

    /**
     * 查询员工档案列表
     */
    @RequiresPermissions("erp:profile:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StaffProfile staffProfile)
    {
        startPage();
        List<StaffProfile> list = staffProfileService.selectStaffProfileList(staffProfile);
        return getDataTable(list);
    }

    /**
     * 导出员工档案列表
     */
    @RequiresPermissions("erp:profile:export")
    @Log(title = "员工档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StaffProfile staffProfile)
    {
        List<StaffProfile> list = staffProfileService.selectStaffProfileList(staffProfile);
        ExcelUtil<StaffProfile> util = new ExcelUtil<StaffProfile>(StaffProfile.class);
        return util.exportExcel(list, "员工档案数据");
    }

    /**
     * 新增员工档案
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存员工档案
     */
    @RequiresPermissions("erp:profile:add")
    @Log(title = "员工档案", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StaffProfile staffProfile)
    {
        staffProfile.setCreateBy(getLoginName());
        return toAjax(staffProfileService.insertStaffProfile(staffProfile));
    }

    /**
     * 修改员工档案
     */
    @RequiresPermissions("erp:profile:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        StaffProfile staffProfile = staffProfileService.selectStaffProfileById(id);
        mmap.put("staffProfile", staffProfile);
        return prefix + "/edit";
    }

    /**
     * 修改保存员工档案
     */
    @RequiresPermissions("erp:profile:edit")
    @Log(title = "员工档案", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StaffProfile staffProfile)
    {
        staffProfile.setUpdateBy(getLoginName());
        return toAjax(staffProfileService.updateStaffProfile(staffProfile));
    }

    /**
     * 删除员工档案
     */
    @RequiresPermissions("erp:profile:remove")
    @Log(title = "员工档案", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(staffProfileService.deleteStaffProfileByIds(ids));
    }
}
